package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TrackLengthVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbTrackInformationBo;
import com.ruoyi.tb.lmt.domain.vo.TbTrackInformationVo;
import com.ruoyi.tb.lmt.domain.TbTrackInformation;
import com.ruoyi.tb.lmt.mapper.TbTrackInformationMapper;
import com.ruoyi.tb.lmt.service.ITbTrackInformationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 基地轨道信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-23
 */
@RequiredArgsConstructor
@Service
public class TbTrackInformationServiceImpl implements ITbTrackInformationService {

    private final TbTrackInformationMapper baseMapper;

    @Autowired
    private TbTrackInformationMapper tbTrackInformationMapper;

    /**
     * 查询基地轨道信息
     */
    @Override
    public TbTrackInformationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询基地轨道信息列表
     */
    @Override
    public TableDataInfo<TbTrackInformationVo> queryPageList(TbTrackInformationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbTrackInformation> lqw = buildQueryWrapper(bo);
        Page<TbTrackInformationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询基地轨道信息列表
     */
    @Override
    public List<TbTrackInformationVo> queryList(TbTrackInformationBo bo) {
        LambdaQueryWrapper<TbTrackInformation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbTrackInformation> buildQueryWrapper(TbTrackInformationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbTrackInformation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlaceId() != null, TbTrackInformation::getPlaceId, bo.getPlaceId());
        lqw.eq(bo.getRequireId() != null, TbTrackInformation::getRequireId, bo.getRequireId());
        lqw.like(StringUtils.isNotBlank(bo.getTrackName()), TbTrackInformation::getTrackName, bo.getTrackName());
        lqw.eq(StringUtils.isNotBlank(bo.getTrackLength()), TbTrackInformation::getTrackLength, bo.getTrackLength());
        lqw.eq(StringUtils.isNotBlank(bo.getDrivewayNumber()), TbTrackInformation::getDrivewayNumber, bo.getDrivewayNumber());
        lqw.eq(bo.getCreateTime() != null, TbTrackInformation::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbTrackInformation::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbTrackInformation::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbTrackInformation::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getTrackNeed()), TbTrackInformation::getTrackNeed, bo.getTrackNeed());
        lqw.eq(StringUtils.isNotBlank(bo.getTrackNeed2()), TbTrackInformation::getTrackNeed2, bo.getTrackNeed2());
        return lqw;
    }

    /**
     * 新增基地轨道信息
     */
    @Override
    public Boolean insertByBo(TbTrackInformationBo bo) {
        TbTrackInformation add = BeanUtil.toBean(bo, TbTrackInformation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改基地轨道信息
     */
    @Override
    public Boolean updateByBo(TbTrackInformationBo bo) {
        TbTrackInformation update = BeanUtil.toBean(bo, TbTrackInformation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbTrackInformation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除基地轨道信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<TrackLengthVo> getTrackLength() {
        return tbTrackInformationMapper.selectTrackLength();
    }
}
